package mall.promotion.impl;

import mall.common.enums.PromotionTypeEnum;
import mall.pojo.ProductDO;
import mall.promotion.PromotionBase;
import mall.promotion.PromotionStrategy;
import mall.utils.ProductJudgeUtil;

import java.util.List;
import java.util.Set;

/**
 * 打折活动 策略实现类
 * @author zmy
 */
public class DiscountPromotion extends PromotionBase implements PromotionStrategy {
//    /** 活动 ID */
//    private String promotionId;
//    /** 活动名字 */
//    private String name;
//    /** 活动类型 */
//    private String promotionType;
//    /** 是否启用 */
//    private boolean enabled;
//    /** 优先级 */
//    private int priority;
//    /** 应用的商品 ID 集合 */
//    private Set<String> productIds;
    /** 打折率： 0.8 表示 8 折 */
    private double discountRate;

    public DiscountPromotion() {}


    public DiscountPromotion(String promotionId, String name, String promotionType, Set<String> productIds, boolean enabled, int priority, double discountRate) {
        super(promotionId, name, promotionType, productIds, enabled, priority);
        this.discountRate = discountRate;
    }

    @Override
    public String getId() {return getPromotionId();}

    @Override
    public boolean isEnabled() {return getEnabled();}

    @Override
    public boolean isApplicable(List<ProductDO> shopCar) {
        if (getPromotionType().equals(PromotionTypeEnum.ALL.type)) {
            return true;
        } else {
            return shopCar.stream()
                    .anyMatch(p -> getProductIds().contains(p.getId()));
        }
    }

    @Override
    public double apply(List<ProductDO> shopCar) {
        shopCar.forEach(p -> {
            // 如果是全场的直接全部应用，不然就判断是否在应用商品列表里
            if (getPromotionType().equals(PromotionTypeEnum.ALL.type) || getProductIds().contains(p.getId())) {
                // 计算打折后的价格
                Double originPrice = p.getPrice();
                p.setPrice(originPrice * discountRate);
            }
        });

        // 打折的商品不额外返现
        return 0;
    }


    @Override
    public String toString() {
        return "DiscountPromotion{" +
                super.toString() +
                "discountRate=" + discountRate +
                "} ";
    }
}
